package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcControllerCardBindMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcControllerCardBind;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.card.ControllerCardBindDto;
import com.csun.cmny.provider.model.dto.card.RfidReaderBindDto;
import com.csun.cmny.provider.model.dto.device.DevicePageQuery;
import com.csun.cmny.provider.model.dto.rfid.RfidReaderDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.card.ControllerCardVo;
import com.csun.cmny.provider.model.vo.rfid.DmcRfidReaderVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcControllerCardBindService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-12-05 16:50
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcControllerCardBindServiceImpl extends BaseService<DmcControllerCardBind> implements DmcControllerCardBindService {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcControllerCardBindMapper dmcControllerCardBindMapper;

    /**
     * query controller card list with page
     *
     * @param devicePageQuery the device page query
     *
     * @return the controller card vo list.
     */
    @Override
    public PageInfo queryListWithPage(DevicePageQuery devicePageQuery) {

        PageHelper.startPage(devicePageQuery.getPageNum(), devicePageQuery.getPageSize());

        /**
         * @date    2020-07-19
         * @author  chisj
         * @desc    修复bug #5124
         */
        // 1,按照条件查询门禁控制卡Id集合 - 分页查询
        List<Long> deviceIdList = dmcDeviceService.selectDeviceIdList(devicePageQuery);
        if (PublicUtil.isEmpty(deviceIdList)) {
            return null;
        }

        PageInfo pageInfo = new PageInfo<>(deviceIdList);

        devicePageQuery.setDeviceIdList(deviceIdList);

        // 2,查询门禁控制卡Id以及对应的读卡器
        PageHelper.startPage(devicePageQuery.getPageNum(), devicePageQuery.getPageSize() * 2);

        pageInfo.setList(dmcControllerCardBindMapper.queryListWithPage(devicePageQuery));

        return pageInfo;
    }

    /**
     * delete controller card by id
     *
     * @param id the controller card id
     * @return the int.
     */
    @Override
    public Integer deleteById(Long id) {

        // 判断是否已经绑定
        DmcControllerCardBind dmcControllerCardBind = new DmcControllerCardBind();
        dmcControllerCardBind.setControllerCardId(id);
        int count = dmcControllerCardBindMapper.selectCount(dmcControllerCardBind);
        if (count > 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC100612001);
        }

        return dmcDeviceService.deleteDmcDeviceById(id);
    }

    /**
     * bind dmc controller card and rfid reader
     *
     * @param controllerCardBindDto the controller card bind dto
     *
     * @return the int
     */
    @Override
    public Integer bind(ControllerCardBindDto controllerCardBindDto) {

        Preconditions.checkArgument(controllerCardBindDto.getControllerCardId() != null,
                cmnyMessageSource.getMessage("controller.card.id.null", null));

        List<RfidReaderBindDto> rfidReaderBindDtoList = controllerCardBindDto.getRfidReaderBindDtoList();
        if (PublicUtil.isEmpty(rfidReaderBindDtoList)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100612002);
        }

        List<DmcControllerCardBind> dmcControllerCardBindList = Lists.newArrayList();
        for (RfidReaderBindDto r : rfidReaderBindDtoList) {
            Preconditions.checkArgument(r.getRfidReaderId() != null,
                    cmnyMessageSource.getMessage("rfid.reader.id.null", null));
            DmcControllerCardBind dmcControllerCardBind = new DmcControllerCardBind();
            dmcControllerCardBind.setControllerCardId(controllerCardBindDto.getControllerCardId());
            dmcControllerCardBind.setRfidReaderId(r.getRfidReaderId());
            dmcControllerCardBind.setNumber(r.getNumber());
            dmcControllerCardBind.setPermission(r.getPermission());

            dmcControllerCardBindList.add(dmcControllerCardBind);
        }

        return this.batchSave(dmcControllerCardBindList);
    }

    /**
     * untied dmc controller card and rfid reader
     *
     * @param dmcControllerCardBind the dmc controller card bind
     * @return the int.
     */
    @Override
    public Integer untied(DmcControllerCardBind dmcControllerCardBind) {

        return dmcControllerCardBindMapper.delete(dmcControllerCardBind);
    }

    /**
     * check rfid reader bind controller card by rfid reader id
     *
     * @param deviceId the rfid reader id
     * @return the int.
     */
    @Override
    public boolean checkRfidReaderBindControllerCardById(Long deviceId) {

        DmcControllerCardBind dmcControllerCardBind = new DmcControllerCardBind();
        dmcControllerCardBind.setRfidReaderId(deviceId);

        int count = dmcControllerCardBindMapper.selectCount(dmcControllerCardBind);

        return count < 1;
    }

    /**
     * query not bind rfid reader
     *
     * @param groupId the group id
     *
     * @return the dmc rfid reader vo list
     */
    @Override
    public List<DmcRfidReaderVo> queryNotBindRfidReader(Long groupId) {

        DevicePageQuery devicePageQuery = new DevicePageQuery();
        devicePageQuery.setGroupId(groupId);
        devicePageQuery.setPageSize(1000);
        devicePageQuery.setDeviceType(DmcDeviceTypeEnum.RFID_READER.getIndex());

        return dmcControllerCardBindMapper.queryNotBindRfidReader(devicePageQuery);
    }

    /**
     * 查询门禁控制Id
     *
     * @param id the rfid reader id
     *
     * @return the controller card
     */
    @Override
    public ControllerCardVo queryControllerCardByRfidReaderId(Long id) {

        return dmcControllerCardBindMapper.queryControllerCardByRfidReaderId(id);
    }

    /**
     * query controller card list with page
     *
     * @param devicePageQuery the device page query
     * @return the controller card vo list.
     */
    @Override
    public List<ControllerCardVo> queryNewListWithPage(DevicePageQuery devicePageQuery) {

        PageHelper.startPage(devicePageQuery.getPageNum(), devicePageQuery.getPageSize());

        return dmcControllerCardBindMapper.queryListWithPage(devicePageQuery);
    }

    /**
     * query controller card by device no
     *
     * @param deviceNo the device no
     *
     * @return the controller card vo
     */
    @Override
    public ControllerCardVo queryControllerCardByDeviceNo(String deviceNo) {

        return dmcControllerCardBindMapper.queryControllerCardByDeviceNo(deviceNo);
    }
}
